/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proiect.fis.commons.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import javax.persistence.*;

/**
 * Tipula de date folosit in aplicatie
 * @author Daniela
 */
@Entity(name="pachet")
@NamedQueries({
    @NamedQuery(name="Pachet.findAll",query="Select e from pachet e"),
})
public class Pachet implements Serializable{
    
    @Id
    @GeneratedValue
    private long id;
    @OneToOne(fetch= FetchType.EAGER,cascade= CascadeType.ALL)
    @JoinColumn(name="expeditor")
    private Persoana expeditor;
    @OneToOne(fetch= FetchType.EAGER,cascade= CascadeType.ALL)
    @JoinColumn(name="destinatar")
    private Persoana destinatar;
    @OneToMany(fetch= FetchType.EAGER)
    @JoinColumn(name="ruta")
    private List<Oras> ruta;
    @Enumerated
    @Column(name="tip")
    private TipPachet tip = TipPachet.NORMAL;
    @Temporal(javax.persistence.TemporalType.DATE)
    @Column(name="dataExpediere")
    private Date dataExpediere;    
    @Temporal(TemporalType.DATE)
    @Column(name="dataSosire")
    private Date dataSosire;
    @Enumerated
    @Column(name="status")
    private StatusPachet status;
    @Column(name="greutate")
    private double gretutate;
    @Column(name="pret")
    private double pret;

    //<editor-fold defaultstate="collapsed" desc="Setters and Getters">
    public Date getDataExpediere() {
        return dataExpediere;
    }
    
    public void setDataExpediere(Date dataExpediere) {
        this.dataExpediere = dataExpediere;
    }
    
    public Date getDataSosire() {
        return dataSosire;
    }
    
    public void setDataSosire(Date dataSosire) {
        this.dataSosire = dataSosire;
    }
    
    public Persoana getDestinatar() {
        return destinatar;
    }
    
    public void setDestinatar(Persoana destinatar) {
        this.destinatar = destinatar;
    }
    
    public Persoana getExpeditor() {
        return expeditor;
    }
    
    public void setExpeditor(Persoana expeditor) {
        this.expeditor = expeditor;
    }
    
    public double getGretutate() {
        return gretutate;
    }
    
    public void setGretutate(double gretutate) {
        this.gretutate = gretutate;
    }
    
    public long getId() {
        return id;
    }
    
    public void setId(long id) {
        this.id = id;
    }
    
    public double getPret() {
        return pret;
    }
    
    public void setPret(double pret) {
        this.pret = pret;
    }
    
    public List<Oras> getRuta() {
        return ruta;
    }
    
    public void setRuta(List<Oras> ruta) {
        this.ruta = ruta;
    }
    
    public StatusPachet getStatus() {
        return status;
    }
    
    public void setStatus(StatusPachet status) {
        this.status = status;
    }
    
    public TipPachet getTip() {
        return tip;
    }
    
    public void setTip(TipPachet tip) {
        this.tip = tip;
    }
    //</editor-fold>
   
    
    
    public String getName(){
        return "Pachet " + this.getId();        
    }

    @Override
    public String toString() {
        return "Pachet{" + "id=" + id + ", expeditor=" + expeditor + ", destinatar=" + destinatar + ", ruta=" + ruta + ", tip=" + tip + ", dataExpediere=" + dataExpediere + ", dataSosire=" + dataSosire + ", status=" + status + ", gretutate=" + gretutate + ", pret=" + pret + '}';
    }
    
    /**
     * returneaza false daca ruta nu a fost creata cu succes, returneaza true 
     * daca ruta a fost creata cu succes
     * @param source
     * @param destination
     * @return 
     */
    public boolean createRute(Oras source,Oras destination){
        Oras current = source;
        if(this.ruta == null){
            ruta = new LinkedList<>();
        }
        ruta.clear();
        while(current != null){
            
            if(current.equals(destination)){
                ruta.add(destination);
                return true;
            }
            ruta.add(current);
            current = current.getConexiune();
            
           
        }
        ruta.clear();
        return false;
    }

}
